<template>
  <div id="app-container">


    <div class="addBox" style="width: 700px;margin: 0 auto;">
      <div class="imgBox" style="float: left">
        <h2>房屋图片</h2>
        <el-upload
          class="avatar-uploader"
          :action="`http://localhost:8087/uploadRoomImg?&id=${room.id}&filePath=room/roomImgs`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="room.img" :src="'http://139.224.186.148:10001/imgs/room/roomImgs/'+room.img" class="avatar">
          <img v-else src="http://139.224.186.148:10001/imgs/default.jpg" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>


    <div style="float: left;margin-left: 50px;margin-top: 40px" >
      <el-form ref="form" label-width="80px">

        <el-form-item label="楼层房号">
          <el-input :value="room.build+'-'+room.number" readonly></el-input>
        </el-form-item>


        <el-form-item label="面积">
          <el-input :value="room.area" readonly></el-input>
        </el-form-item>
        <el-form-item label="朝向">
          <el-input v-model="room.direction" readonly></el-input>
        </el-form-item>

        <el-form-item label="室厅">
          <el-input v-model="room.roomType" readonly></el-input>
        </el-form-item>


      </el-form>
    </div>

    <div style="width: 650px;clear: left">
      <h3>简介</h3>
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入内容"
        v-model="room.introduction"
        style="width: 500px"
        :autosize="{ minRows: 2, maxRows: 5}"
       >
      </el-input>
      <el-button  @click="updateIntroduction()">更新简介</el-button>
      </div>
      <p/>
    <div style="width: 650px;height:200px;bottom: 1px">
      <h3>位置</h3>
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入内容"
        style="width: 500px"
        v-model="room.position"
        :autosize="{ minRows: 2, maxRows: 4}">
      </el-input>
      <el-button  @click="updatePosition()">更新位置</el-button>
    </div>
  </div>
  </div>
</template>

<script>
import Room from "@/api/room";

export default {
  name: "add.vue",
  data() {
    return {
      room: {},
      imageUrl: '',
      addRoomForm: {
        quartersId: '',
        ownerId: '',
        rent: '',
        type: '',
        floor: '',
        startTime: '',
        endTime: '',
        img: '',
        number: ''
      },
      addRoomTemp: {
        lou: '',
        cen: '',
        date: []
      },
      quarters: [],
      owners: []
    }
  },
  methods: {
    updateIntroduction(){
      const id = this.$route.params.id;
      let room = {'id':id,'introduction':this.room.introduction}
      Room.editInfo(room).then(res => {
        alert("更新成功")
      })
    },
    updatePosition(){
      const id = this.$route.params.id;
      let room = {'id':id,'position':this.room.position}
      Room.editInfo(room).then(res => {
        alert("更新成功")
        this.initRoomInfo()
      })
    },
    initRoomInfo() {
      const id = this.$route.params.id; //得到路由地址的参数,参数名为自己设置的 占位符
      console.log(id)
      Room.getRoom(id).then(res => {
        console.log(res)
        this.room = res.data.roomInfo
      })
    },
    addRoom() {
      this.addRoomForm.floor = this.addRoomTemp.lou + '/' + this.addRoomTemp.cen
      console.log(this.addRoomTemp.date[0])
      console.log(this.addRoomTemp.date[1])
      this.addRoomForm.startTime = this.addRoomTemp.date[0];
      this.addRoomForm.endTime = this.addRoomTemp.date[1];
      console.log(this.addRoomForm)
      Room.addRoom(this.addRoomForm).then(res => {
        alert(res)
      })

    },
    initOwner() {
      Room.getOwners().then(res => {
        const {data} = res
        this.owners = data.ownerList
      })

    },
    initQuarters() {
      Room.getQuarters().then(res => {
        const {data} = res
        this.quarters = data.quarterList
      })
    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      const {data} = res
      console.log(data)
      this.addRoomForm.img = data
      this.initRoomInfo()

    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  created() {
    // this.initQuarters()
    // this.initOwner()
    this.initRoomInfo()
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
